﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Product;
using Telerik.Windows.Controls;
using BrokerBase.Lib.Tool;
using BrokerBase.Lib.Policy;
using NHibernate;

namespace Broker.View.PolicyView
{
    /// <summary>
    /// RateColSelectForm.xaml 的交互逻辑
    /// </summary>
    public partial class RateColSelectForm 
    {
        private IPolicy policy;

        IList<RadButton> btns = new List<RadButton>();

        private IDictionary<string, string> result;

        public IDictionary<string, string> Result { get { return result; } }

        public RateColSelectForm(IPolicy policy)
        {
            this.policy = policy;
            InitializeComponent();
            Loaded += new RoutedEventHandler(RateColSelectForm_Loaded);
        }

        void RateColSelectForm_Loaded(object sender, RoutedEventArgs e)
        {
            SetFixColRate(this.policy.Product.ColRateFactors);

            this.Height = this.policy.Product.ColRateFactors.Count() * 25 + 40;

            rbtnVer.IsChecked = true;
        }

        private void SetFixColRate(IList<IRateFactor> colFactors)
        {
            try
            {
                IList<string[]> colItems = new List<string[]>();

                ISession sess = LibGlobal.HbmSessionFactory.GetCurrentSession();

                int rowCount = colFactors.Count();

                IList<object[]> rowRates = null;

                if (policy.SelectedRowRateFactors.Count ==2)
                {
                    string key1 = policy.SelectedRowRateFactors[policy.Product.RowRateFactors[0].Name];

                    string key2 = policy.SelectedRowRateFactors[policy.Product.RowRateFactors[1].Name];

                    rowRates = PolicyUtil.LoadRowRate(this.policy.Product, key1, key2,sess);
                }

                for (int k = 0; k < rowCount; k++)
                {
                    IRateFactor rf = colFactors[k];
                    colItems.Add(rf.Items.Split(';'));

                    RowDefinition rdf = new RowDefinition();
                    rdf.Height = new GridLength(25);
                    gdRateHead.RowDefinitions.Add(rdf);

                    TextBlock tbk = new TextBlock();
                    tbk.Text = rf.Name;
                    tbk.SetValue(Grid.RowProperty, k);
                    gdRateHead.Children.Add(tbk);

                }

                int colCount = colItems[0].Count();

                for (int i = 0; i < colCount; i++)
                {
                    IDictionary<string, string> colComb = new Dictionary<string, string>();

                    ColumnDefinition col = new ColumnDefinition();
                    col.Width = GridLength.Auto;

                    gdRateItem.ColumnDefinitions.Add(col);

                    RadButton btn = new RadButton();

                    if (i != colCount - 1)
                    {
                        btn.BorderThickness = new Thickness(1, 1, 0, 1);
                    }
                    Grid g = new Grid();
                    btn.Content = g;

                    for (int j = 0; j < rowCount; j++)
                    {
                        RowDefinition rf = new RowDefinition();

                        rf.Height = new GridLength(25);
                        g.RowDefinitions.Add(rf);

                        TextBlock tb = new TextBlock();
                        tb.Text = colItems[j][i];
                        tb.SetValue(Grid.RowProperty, j);
                        g.Children.Add(tb);


                        colComb.Add(colFactors[j].Name, colItems[j][i]);
                    }

                    btn.Tag = colComb;
                    btn.Click += new RoutedEventHandler(btn_Click);
                    btn.SetValue(Grid.ColumnProperty, i);
                    gdRateItem.Children.Add(btn);

                    btns.Add(btn);

                    IDictionary<string, string> result = PolicyUtil.VerifyPoliyFixRate(this.policy, colComb, sess, rowRates);

                    if (result.Count() > 0)
                    {
                        btn.IsEnabled = false;

                        
                        //string tips = null;
                        //foreach (KeyValuePair<string, string> v in result)
                        //{
                        //    tips += Environment.NewLine + v.Value;
                        //}

                        //btn.ToolTip = tips;
                    }

                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {

            IDictionary<string, string> cmbs = (IDictionary<string,string>)(sender as RadButton).Tag;

            this.result = cmbs;
            this.Close();
        }

        private void RadRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadRadioButton r = sender as RadRadioButton;

            

            foreach (RadButton btn in btns)
            {
                if (!btn.IsEnabled)
                {
                    if (r.Name == "rbtnAll")
                    {
                        btn.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        btn.Visibility = Visibility.Collapsed;
                    }
                }
            }
        }
        
    }
}
